package com.nth.rooster.authentication.services;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nth.rooster.authentication.beans.LoginBean;
import com.nth.rooster.authentication.beans.RegistrationBean;
import com.nth.rooster.authentication.dao.UserDaoI;
import com.nth.rooster.core.comparator.MenuBeanComparator;
import com.nth.rooster.core.constants.RoosterConstants;
import com.nth.rooster.core.entities.RamUserCircleMember;
import com.nth.rooster.core.entities.RamCircles;
import com.nth.rooster.core.entities.RamLogins;
import com.nth.rooster.core.entities.RamMenus;
import com.nth.rooster.core.entities.RamRoles;
import com.nth.rooster.core.entities.RamUsers;
import com.nth.rooster.core.pojo.CircleBeans;
import com.nth.rooster.core.pojo.MenuBean;
import com.nth.rooster.core.pojo.User;

@Service
public class UsersService implements UsersServiceI{
	
	static Logger LOGGER = Logger.getLogger(UsersService.class);
	
	@Autowired
	UserDaoI userDaoI;

	public List<User> getAllInternalUsers() {
		LOGGER.debug("Entered::Authentication::UsersService::getAllInternalUsers");
		List<RamUsers> users= userDaoI.getUsersBasedOnActiveStatus(Boolean.TRUE);
		List<User> internalUsers = new ArrayList<User>();
		if (users!= null && !users.isEmpty()) {
			for (RamUsers ramUsers : users) {
				User user = new User();
				user.setUserID(ramUsers.getUserId());
				user.setFirstName(ramUsers.getUserFirstName());
				user.setLastName(ramUsers.getUserLastName());
				internalUsers.add(user);
			}
			
		}
		LOGGER.debug("Exited::Authentication::UsersService::getAllInternalUsers");
		return internalUsers;
	}

	public void saveRegistration(RegistrationBean registrationBean) {
		
		LOGGER.debug("Entered::Authentication::UsersService::saveRegistration");
		RamUsers ramUser= null;
		if (registrationBean.getUserID()==null) {
			 ramUser = new RamUsers();	
		}else{
			ramUser = userDaoI.getUserData(registrationBean.getUserID());
		}
		
		ramUser.setUserFirstName(registrationBean.getFirstName());
		ramUser.setUserLastName(registrationBean.getLastName());
		ramUser.setActive(true);
		ramUser.setIsInternal(registrationBean.getIsInternal());
		Set<RamRoles> ramRoles = new HashSet<RamRoles>();
		RamRoles ramRole = new RamRoles();
	
		if (!registrationBean.getIsInternal()) {
			ramRole.setRoleId(RoosterConstants.PORTAL_ROLE_ID);
		}else{
			ramRole.setRoleId(RoosterConstants.ADMIN_ROLE_ID);
		}
	
		ramRoles.add(ramRole);
		ramUser.setRamRoles(ramRoles);
		
		
		Set<RamUserCircleMember> circles = null;
		if (registrationBean.getCircleRequestedForAuthorization() != null) {
			circles = new HashSet<RamUserCircleMember>();
			RamUserCircleMember ramCircleUser = new RamUserCircleMember();
			for (CircleBeans circleBeans : registrationBean.getCircleRequestedForAuthorization()) {
				RamCircles ramCircles = new RamCircles();
				ramCircles.setCircleID(circleBeans.getCircleID());
				ramCircleUser.setCircleID(ramCircles);
				ramCircleUser.setUserID(ramUser);
				circles.add(ramCircleUser);
			}
		}
		ramUser.setMemberForcircles(circles);
		
		ramUser = userDaoI.saveRegistration(ramUser);
		if(registrationBean.getUserID()==null){
			RamLogins logins = new RamLogins();
			logins.setRamUsers(ramUser);
			logins.setUsername(registrationBean.getUserName());
			logins.setPassword(registrationBean.getPassword());
			userDaoI.saveLoginCredentials(logins);
			
		}
		
		LOGGER.debug("Exited::Authentication::UsersService::saveRegistration");
		
	}
	
	public RegistrationBean getUserData(Long userID) {
		LOGGER.debug("Entered::Authentication::UsersService::getUserData::userID" + userID);
		RamUsers ramUsers = userDaoI.getUserData(userID);
		RegistrationBean bean = new RegistrationBean();
		bean.setFirstName(ramUsers.getUserFirstName());
		bean.setLastName(ramUsers.getUserLastName());
		bean.setIsInternal(ramUsers.getIsInternal());
		
		List<String> listOfRoles = new ArrayList<String>();
		for (RamRoles roles : ramUsers.getRamRoles()) {
			listOfRoles.add(roles.getRoleName());
		}
		
		List<CircleBeans> circleBeans = new ArrayList<CircleBeans>();
		
		for (RamUserCircleMember circle : ramUsers.getMemberForcircles()) {
			
			CircleBeans beans = new CircleBeans();
			beans.setCircleID(circle.getCircleID().getCircleID());
			beans.setCircleTitle(circle.getCircleID().getCircleName());
			beans.setCircleDescrition(circle.getCircleID().getCircleDescription());
			
			circleBeans.add(beans);
			
		}
		
		/**
		 * @TODO
		 * 
		 * Owner of circle, should also be populated. 
		 */
		
		bean.setCircleRequestedForAuthorization(circleBeans);
		bean.setRoles(listOfRoles);
		
		return bean;
		
	}

	public Set<MenuBean> getUserMenu(Long userID) {
		
		LOGGER.debug("Entered::Authentication::UsersService::getUserMenu::UserID::" + userID);
		
		Set<RamRoles> ramRoles=userDaoI.getUserMenu(userID);
		
		Set<MenuBean> menus = new TreeSet<MenuBean>(new MenuBeanComparator());
		for (RamRoles roles : ramRoles) {
			
			if(null!= roles.getRamMenus()){
				for (RamMenus menu : roles.getRamMenus()) {
					MenuBean bean = new MenuBean();
					bean.setActionID(menu.getActionID());
					bean.setActionName(menu.getActionName());
					bean.setActionDescriton(menu.getActionDescription());
					menus.add(bean);
				}
			}
			
		}
		LOGGER.debug("Exited::Authentication::UsersService::getUserMenu::UserID::" + userID);
		return menus;
	}

	public void updateUserCredentials(LoginBean loginBean) {
		LOGGER.debug("Entered::Authentication::UsersService::updateUserCredentials::UserID::" + loginBean.getUserID());
		
		
		
		LOGGER.debug("Entered::Authentication::UsersService::updateUserCredentials::UserID::" + loginBean.getUserID());
		
	}

	
	
}
